package main

import (
	"EventsApp"
	"EventsApp/pkg/handler"
  DB "EventsApp/pkg/db"
  Schemas "EventsApp/pkg/db/schemas"

	"log"

	"github.com/spf13/viper"
)

func main() {
  if err := initConfig(); err != nil {
		log.Fatalf("Error with initializing configs: %s", err.Error())
	}

  db, err := DB.InitPostgresDB(
    DB.Config{
      Host: viper.GetString("db.host"),
      Port: viper.GetString("db.port"),
      Username: viper.GetString("db.username"),
      DBName: viper.GetString("db.dbname"),
      SSLMode: viper.GetString("db.sslmode"),
      Password: viper.GetString("db.password"),
  })

  db.AutoMigrate(&Schemas.User{})

  if err != nil {
    log.Fatalf("Error with initializing DB: %s", err.Error())
  }

	srv := new(EventsApp.Server)
	if err := srv.Run(viper.GetString("port"), handler.InitRoutes(db)); err != nil {
		log.Fatalf("Error: %s", err.Error())
	}
}

func initConfig() error {
  viper.AddConfigPath("configs")
  viper.SetConfigName("config")

  return viper.ReadInConfig()
}
